$(document).ready(function () {
    $('.play_btn').animate({
        'opacity': 1
    }, 1350);
    init_events();
});

function init_events() {
    /*Play button event start*/
    $('.play_container').on('click', '.play_btn', function () {
        $('.start_overlay').animate({
            'opacity': 0
        }, 500, function () {
            $('.start_overlay').hide();
            $('.wrapper').animate({
                'opacity': 1
            }, 500, function () {
                start_game();
            });
        });
    });
    /*Play button event end*/

    /*Sound control button event start*/
    $('.right_content').on('click', '.sound_control', function () {
        $(this).toggleClass('sound_control_off');
    });
    /*Sound control button event end*/

    /*fire bubble event start*/
    $('.game_wrapper').on('click', '.bubbles_wrapper', function () {
        change_next_ball();
    });
    /*fire bubble event end*/

    /*restart game btn event start*/
    $('.end_game').on('click', '.restart_btn', function () {
        reset_game();
    });
    /*restart game btn event end*/
}

function start_game() {
    start_timer(true)
    start_timer(false);
    change_next_ball();
}

function start_timer(flag) {
    var seconds = 5;
    if (flag == true) {
        setInterval(function () {
            if (seconds == 1) {
                seconds = 5;
            } else {
                seconds--;
            }
            $('.timer_text').html('Time: ' + seconds);
        }, 1000);
        $('.inside_loader').animate({
            'width': '100%'
        }, 5000, function () {

            $('.inside_loader').css('width', '0%');
        });
    } else {
        var interval = setInterval(function () {

            $('.inside_loader').animate({
                'width': '100%'
            }, 5000, function () {

                $('.inside_loader').css('width', '0%');
            });
        }, 5000);
    }
}

function change_next_ball() {
    $('.next').animate({
        'right': '50%'
    }, 200);
}

function end_game() {
    $('.end_game').show();
    $('.end_game').animate({
        'opacity': 1
    }, 250);
}

function reset_game() {
    $('.bubbles_wrapper').html('');
    window.clearInterval();
    change_next_ball();
    $('.end_game').animate({
        'opacity': 0
    }, 250, function () {
        $('.end_game').hide();
    });
}